/* $Id: main.c,v 1.2 2006-10-23 17:30:55 nicolasw Exp $ */

#include <stdio.h>
#include <stdlib.h>
#include <time.h> /* Para srand */
#include <assert.h>
#include "bitmap.h"

#define SIZE 10000
#define ELEMS 1000

int main(void)
{
	size_t new=0,i=0,j=0;
	size_t elems[ELEMS];
	bitmap_s *bitmap = bitmap_create(SIZE);
	if(bitmap == NULL) return 1;
	srand(time(NULL));
	for(i=0; i!= ELEMS;i++)
	{
		j=0;
		new = rand() % SIZE;
		while(j!=i && elems[j] != new) j++;
		while(j!=i && elems[j] == new)
		{
			j=0;
			new = rand() % SIZE;
			while(j!=i && elems[j] != new) j++;
		} 
		/* Notar que el ciclo de arriba podria no terminar nunca */
		elems[i] = new;
	}

	assert(bitmap_size(bitmap) == 0 && bitmap_empty(bitmap));
	assert(bitmap_capacity(bitmap) >= SIZE);

	for(i=0; i != ELEMS; i++)
	{
		printf("Insert %i\n",elems[i]);
		bitmap_insert(bitmap,elems[i]);
		assert(bitmap_size(bitmap) == i+1 && !bitmap_empty(bitmap));
	}
	
	assert(bitmap_capacity(bitmap) >= SIZE);

	for(i=0; i != ELEMS; i++)
	{
		printf("Remove %i\n",elems[i]);
		assert(bitmap_member(bitmap,elems[i]));
		bitmap_remove(bitmap,elems[i]);
		assert(bitmap_size(bitmap) == ELEMS-i-1);
		if(i != ELEMS-1) assert(!bitmap_empty(bitmap));
	}

	assert(bitmap_size(bitmap) == 0 && bitmap_empty(bitmap));
	assert(bitmap_capacity(bitmap) >= SIZE);

	for(i=0; i != ELEMS; i++)
	{
		printf("Insert %i\n",elems[i]);
		bitmap_insert(bitmap,elems[i]);
		assert(bitmap_size(bitmap) == i+1 && !bitmap_empty(bitmap));
	}
	
	assert(bitmap_capacity(bitmap) >= SIZE);

	for(i=ELEMS-1; i != 0; i--)
	{
		printf("Remove %i\n",elems[i]);
		assert(bitmap_member(bitmap,elems[i]));
		bitmap_remove(bitmap,elems[i]);
		assert(bitmap_size(bitmap) == i  && !bitmap_empty(bitmap));
	}
	printf("Remove %i\n",elems[i]);
	assert(bitmap_member(bitmap,elems[i]));
	bitmap_remove(bitmap,elems[i]);
	assert(bitmap_size(bitmap) == i  && bitmap_empty(bitmap));
	       
	bitmap_destroy(bitmap);

	return 0;
}
